package com.ai.service.made.impl;

import com.ai.entity.made.Cameras;
import com.ai.entity.made.QbeeTemplate;
import com.ai.entity.made.QbeeTemplateConfiguration;
import com.ai.entity.made.camera.CameraPO;
import com.ai.mapper.made.QbeeTemplateConfigurationMapper;
import com.ai.model.param.QbeeTemplateParam;
import com.ai.service.made.CameraService;
import com.ai.service.made.ICamerasService;
import com.ai.service.made.IQbeeTemplateConfigurationService;
import com.ai.service.made.IQbeeTemplateService;
import com.ai.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2024-07-29
 */
@Service
public class QbeeTemplateConfigurationServiceImpl extends ServiceImpl<QbeeTemplateConfigurationMapper, QbeeTemplateConfiguration> implements IQbeeTemplateConfigurationService {

    @Resource
    private   CameraService cameraService;


    @Resource
    private IQbeeTemplateService iQbeeTemplateService;


    @Override
    public void saveConfiguration(QbeeTemplate qbeeTemplate, QbeeTemplateParam qbeeTemplateParam) {

        for (String cameraId : qbeeTemplateParam.getCameraIdList()) {

            //新增摄像头模板配置关系
            QbeeTemplateConfiguration templateConfiguration = new QbeeTemplateConfiguration();
            templateConfiguration.setQbeeTemplateId(qbeeTemplate.getId().toString());
            templateConfiguration.setCameraId(cameraId);
            templateConfiguration.setCreateTime(LocalDateTime.now());
            templateConfiguration.setUpdateTime(LocalDateTime.now());
            templateConfiguration.setCreateUser(SecurityUtils.getLoginUserName());
            templateConfiguration.setUpdateUser(SecurityUtils.getLoginUserName());

            save(templateConfiguration);
        }

    }

    @Override
    public List<CameraPO> getCamerListByTemplateId(String id) {

        List<QbeeTemplateConfiguration> templateConfigurationList = this.list(new QueryWrapper<QbeeTemplateConfiguration>().eq("qbee_template_id", id));
        List<CameraPO> cameraPOList = templateConfigurationList.stream().map(qbeeTemplateConfiguration -> {
            CameraPO cameraPO = cameraService.getById(qbeeTemplateConfiguration.getCameraId());
            return cameraPO;
        }).collect(Collectors.toList());

        return cameraPOList;
    }

    @Override
    public void updateCameraConfigrationInfo(QbeeTemplateParam qbeeTemplateParam) {

        //获取当前模板下的所有摄像头
        List<QbeeTemplateConfiguration> configurationList =
                this.list(new QueryWrapper<QbeeTemplateConfiguration>().eq("qbee_template_id", qbeeTemplateParam.getId()));
        List<String> oldCameraIds =
                configurationList.stream().map(QbeeTemplateConfiguration::getCameraId).collect(Collectors.toList());
        List<String> newCameraList = qbeeTemplateParam.getCameraIdList();

        //先取交集
        ArrayList<String> retainCameraIdList = new ArrayList<>(oldCameraIds);
        retainCameraIdList.retainAll(newCameraList);

        //计算需要新增的数据
        newCameraList.removeAll(retainCameraIdList);
        //计算需要删除的数据
        oldCameraIds.removeAll(retainCameraIdList);

        if (!CollectionUtils.isEmpty(oldCameraIds)){
            //开始删除数据
            QueryWrapper<QbeeTemplateConfiguration> configurationQueryWrapper = new QueryWrapper<>();
            configurationQueryWrapper.eq("qbee_template_id",qbeeTemplateParam.getId()).in("camera_id",oldCameraIds);

            this.remove(configurationQueryWrapper);
        }

        if (!CollectionUtils.isEmpty(newCameraList)) {
            //开始新增数据
            saveConfiguration(new QbeeTemplate().setId(Integer.valueOf(qbeeTemplateParam.getId())), new QbeeTemplateParam().setCameraIdList(newCameraList));
        }
    }

    @Override
    public List<String> getTemplateIdByCameraId(Integer id) {
        List<String> qbeeTemplateIds =new ArrayList<>();
        List<QbeeTemplateConfiguration> configurationList = list(new QueryWrapper<QbeeTemplateConfiguration>().eq("camera_id", id));
        if (!CollectionUtils.isEmpty(configurationList)){
            List<String> templateConfigId = configurationList.stream().map(QbeeTemplateConfiguration::getQbeeTemplateId).distinct().collect(Collectors.toList());
            List<QbeeTemplate> qbeeTemplateList = iQbeeTemplateService.list(new QueryWrapper<QbeeTemplate>().in("id", templateConfigId));
            qbeeTemplateIds = qbeeTemplateList.stream().map(QbeeTemplate::getTemplateId).distinct().collect(Collectors.toList());
        }
        return qbeeTemplateIds;
    }
}
